package com.suyuan.controller.shop;

import com.suyuan.entity.CustomCartItem;
import com.suyuan.entity.CustomInfo;
import com.suyuan.entity.CustomerCart;
import com.suyuan.params.ChangeItemParam;
import com.suyuan.service.CustomCartService;
import com.suyuan.utils.JsonMsg;
import com.suyuan.utils.JsonUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * 购物车/购物项控制类
 */
@Controller
@RequestMapping(value = "/shop/customCart")
public class CustomCartController {
    @Resource(name = "customCartService")
    private CustomCartService customCartService;

    /**
     * 我的购物车
     *
     */
    @RequestMapping(value = "myCart")
    @ResponseBody
    public Object myCart(@RequestBody String data){
        CustomInfo customer = JsonUtils.jsonToPojo(data, CustomInfo.class);
        if(customer.getGid() == null){
            return JsonMsg.ErrorMsg("参数不能为空");
        }
        return JsonMsg.SuccessMsg(customCartService.findByCusId(customer.getGid()));
    }

    /**
     * 添加购物车
     *
     */
    @RequestMapping(value = "/addCartItem",method = RequestMethod.POST)
    @ResponseBody
    public Object addCartItem(@RequestBody String data){
        //必传cartId、productId、productCount
        System.out.println("data:"+data);
        CustomCartItem item = JsonUtils.jsonToPojo(data, CustomCartItem.class);
        System.out.println("item:"+item);
        try {
            customCartService.addCartItem(item);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonMsg.ErrorMsg(e.getMessage());
        }
        return JsonMsg.SuccessMsg(null);
    }

    /**
     * 移除购物项
     *
     */
    @RequestMapping(value = "/removeItem")
    @ResponseBody
    public Object removeItem(@RequestBody String data){
        CustomCartItem cartItem = JsonUtils.jsonToPojo(data, CustomCartItem.class);
        if(cartItem.getId() == null){
            return JsonMsg.ErrorMsg("参数不能为空");
        }
        boolean isSuccess = false;
        isSuccess = customCartService.deleteById(cartItem.getId());
        if(isSuccess){
            return JsonMsg.SuccessMsg(null);
        }
       return JsonMsg.ErrorMsg("你所删除的对象不存在");
    }


    /**
     * 清空购物车
     *
     */
    @RequestMapping(value = "/clearCart")
    @ResponseBody
    public Object clearCart(@RequestBody String data){
        CustomerCart cart = JsonUtils.jsonToPojo(data, CustomerCart.class);
        if(cart.getCartId() == null){
            return JsonMsg.ErrorMsg("参数不能为空");
        }
        boolean isSuccess = false;
        isSuccess = customCartService.clearCart(cart.getCartId());
        if(isSuccess){
            return JsonMsg.SuccessMsg(null);
        }
        return JsonMsg.ErrorMsg("你所删除的对象不存在");
    }

    /**
     * 更改购物车中商品数量
     */
    @RequestMapping(value = "/changeItemCount")
    @ResponseBody
    public Object changeItemCount(@RequestBody String data){
        ChangeItemParam param = JsonUtils.jsonToPojo(data, ChangeItemParam.class);
        if(param.getId() == null || param.getCount() == null){
            return JsonMsg.ErrorMsg("参数不能为空");
        }
        try {
            customCartService.changeItemCount(param.getId(),param.getCount());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonMsg.ErrorMsg(e.getMessage());
        }
        return JsonMsg.SuccessMsg(null);
    }
}
